<template>
  <div style="width: 80%; margin: 0 auto">
    <el-card shadow="always">
      <el-form label-width="140px" style="width: 80%; margin: 0 auto">
        <el-form-item
          style="border-bottom: 1px solid #ebeef5"
          label-width="0px"
        >
          <el-form-item
            label="公司等级："
            style="width: 50%; display: inline-block"
          >
            <input
              style="width: 100%"
              v-if="companyList != null && companyList.companyLevel == 1"
              value="全球公司"
              type="text"
              readonly
            />
            <input
              style="width: 100%"
              v-else-if="companyList != null && companyList.companyLevel == 2"
              value="省级公司"
              type="text"
              readonly
            />
            <input
              style="width: 100%"
              v-else-if="companyList != null && companyList.companyLevel == 3"
              value="市级公司"
              type="text"
              readonly
            />
            <input
              style="width: 100%"
              v-else-if="companyList != null && companyList.companyLevel == 4"
              value="县级公司"
              type="text"
              readonly
            />
            <input style="width: 100%" v-else value="无" type="text" readonly />
          </el-form-item>
        </el-form-item>
        <el-form-item
          style="border-bottom: 1px solid #ebeef5"
          label-width="0px"
        >
          <el-form-item
            label="公司类型："
            style="width: 50%; display: inline-block"
          >
            <input
              style="width: 100%"
              v-if="companyList != null && companyList.companyType == 0"
              value="孵化公司"
              type="text"
              readonly
            />
            <input
              style="width: 100%"
              v-else-if="companyList != null && companyList.companyType == 1"
              value="临时公司"
              type="text"
              readonly
            />
            <input
              style="width: 100%"
              v-else-if="companyList != null && companyList.companyType == 2"
              value="正式公司"
              type="text"
              readonly
            />
            <input style="width: 100%" v-else value="无" type="text" readonly />
          </el-form-item>
          <el-form-item
            label="公司名称："
            style="width: 50%; display: inline-block"
          >
            <input
              style="width: 100%"
              v-if="companyList != null && companyList.companyName"
              v-model="companyList.companyName"
              type="text"
              readonly
            />
            <input style="width: 100%" v-else value="无" type="text" readonly />
          </el-form-item>
        </el-form-item>
        <el-form-item
          style="border-bottom: 1px solid #ebeef5"
          label-width="0px"
        >
          <el-form-item
            label="创始人："
            style="width: 50%; display: inline-block"
          >
            <input
              style="width: 100%"
              v-if="companyList != null && companyList.creatorAccount"
              v-model="companyList.creatorAccount"
              type="text"
              readonly
            />
            <input style="width: 100%" v-else value="无" type="text" readonly />
          </el-form-item>
        </el-form-item>
        <el-form-item
          style="border-bottom: 1px solid #ebeef5"
          label-width="0px"
        >
          <el-form-item
            label="首席合伙人："
            style="width: 50%; display: inline-block"
          >
            <input
              style="width: 100%"
              v-if="companyList != null && companyList.chiefPartnerAccount"
              v-model="companyList.chiefPartnerAccount"
              type="text"
              readonly
            />
            <input style="width: 100%" v-else value="无" type="text" readonly />
          </el-form-item>
          <el-form-item
            label="首席合伙人类型："
            style="width: 50%; display: inline-block"
          >
            <input
              style="width: 100%"
              v-if="companyList != null && companyList.companyType == 0"
              value="孵化"
              type="text"
              readonly
            />
            <input
              style="width: 100%"
              v-else-if="companyList != null && companyList.companyType == 1"
              value="临时"
              type="text"
              readonly
            />
            <input
              style="width: 100%"
              v-else-if="companyList != null && companyList.companyType == 2"
              value="正式"
              type="text"
              readonly
            />
            <input style="width: 100%" v-else value="无" type="text" readonly />
          </el-form-item>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 已有首席合伙人变更申请-->
    <el-card shadow="always" style="margin-top: 20px">
      <div class="title">已有首席合伙人变更申请</div>
      <el-form label-width="140px" style="width: 80%; margin: 0 auto">
        <el-form-item
          style="border-bottom: 1px solid #ebeef5"
          label-width="0px"
        >
          <el-form-item
            label="申请状态："
            style="width: 50%; display: inline-block"
          >
            <input
              v-if="formData.auditstatus == 0"
              style="width: 100%"
              value="待审核"
              type="text"
              readonly
            />
            <input
              v-else-if="formData.auditstatus == 1"
              style="width: 100%"
              value="待生效"
              type="text"
              readonly
            />
            <input
              v-else-if="formData.auditstatus == 2"
              style="width: 100%"
              value="驳回"
              type="text"
              readonly
            />
            <input
              v-else-if="formData.auditstatus == 3"
              style="width: 100%"
              value="已生效"
              type="text"
              readonly
            />
            <input v-else style="width: 100%" value="无" type="text" readonly />
          </el-form-item>
        </el-form-item>
        <el-form-item
          style="border-bottom: 1px solid #ebeef5"
          label-width="0px"
        >
          <el-form-item
            label="申请人信息："
            style="width: 50%; display: inline-block"
          >
            <span v-if="formData.applyAccount != null"
              ><span>{{ formData.applyAccount }}</span
              ><span style="margin-left: 10px">{{
                formData.applyEnterpriseName
              }}</span></span
            >
            <span v-else>无</span>
          </el-form-item>
        </el-form-item>
        <el-form-item
          style="border-bottom: 1px solid #ebeef5"
          label-width="0px"
        >
          <el-form-item
            label="变更后首席合伙人："
            style="width: 50%; display: inline-block"
          >
            <span v-if="formData.ceoAccount != null"
              ><span>{{ formData.ceoAccount }}</span
              ><span style="margin-left: 10px">{{
                formData.ceoEnterpriseName
              }}</span></span
            >
            <span v-else>无</span>
          </el-form-item>
        </el-form-item>
        <el-form-item
          style="width: 100%; display: inline-block"
          label-width="0px"
        >
          <el-form-item
            label="申请时间："
            style="width: 33%; display: inline-block"
          >
            <i class="el-icon-time" v-if="formData.createtime != null"></i>
            <span v-if="formData.createtime != null">{{
              formData.createtime | formatDateStr("yyyy-MM-dd hh:mm:ss")
            }}</span>
            <span v-else>无</span>
          </el-form-item>
          <el-form-item
            label="审核时间："
            style="width: 33%; display: inline-block"
          >
            <i class="el-icon-time" v-if="formData.audittime != null"></i>
            <span v-if="formData.audittime != null">{{
              formData.audittime | formatDateStr("yyyy-MM-dd hh:mm:ss")
            }}</span>
            <span v-else>无</span>
          </el-form-item>
          <el-form-item
            label="生效时间："
            style="width: 33%; display: inline-block"
          >
            <i class="el-icon-time" v-if="formData.effectivetime != null"></i>
            <span v-if="formData.effectivetime != null">{{
              formData.effectivetime | formatDateStr("yyyy-MM-dd hh:mm:ss")
            }}</span>
            <span v-else>无</span>
          </el-form-item>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 新增首席合伙人变更申请 -->

    <el-card shadow="always" style="margin-top: 20px">
      <div class="title">新增首席合伙人变更申请</div>
      <el-form
        label-width="150px"
        style="width: 80%; margin: 0 auto"
        :rules="rules"
        ref="partnerform"
        :model="partnerform"
      >
        <el-form-item label-width="0px">
          <el-form-item
            label="首席合伙人变更为："
            prop="mobile"
            style="width: 40%; display: inline-block"
          >
            <el-input
              style="width: 100%"
              v-model="partnerform.mobile"
              size="mini"
              readonly
              placeholder="账号"
            />
          </el-form-item>
          <el-form-item
            style="
              width: calc(40% - 150px);
              display: inline-block;
              margin-left: 20px;
            "
            prop="companyname"
          >
            <el-input
              style="width: 100%"
              v-model="partnerform.companyname"
              size="mini"
              readonly
              placeholder="商家公司名称"
            />
          </el-form-item>
        </el-form-item>
        <el-form-item label="" style="width: 100%; display: inline-block">
          <el-button size="mini" type="primary" @click="handleCompanyForm"
            >可变更合伙人查询</el-button
          >
          <!-- <el-button v-else size="mini" type="primary" disabled>可变更合伙人查询</el-button> -->
          <!-- <el-button  size="mini" type="danger" @click="onSubmitPartner('partnerform')">首席合伙人变   更</el-button> -->
          <el-button
            v-if="
              companyList.companyType == 2 &&
              formData.auditstatus != 0 &&
              formData.auditstatus != 1
            "
            size="mini"
            type="danger"
            @click="onSubmitPartner('partnerform')"
            >首席合伙人变更</el-button
          >
          <el-button v-else size="mini" type="danger" disabled
            >首席合伙人变更</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>

    <!--公司详情-->
    <el-dialog
      title="公司信息"
      :visible.sync="companyVisible"
      :before-close="hideCompanyForm"
      width="50%"
      top="5vh"
      v-dialogDrag
      :close-on-click-modal="false"
    >
      <template slot="title">
        <div class="avue-crud__dialog__header">
          <span class="el-dialog__title">
            <span
              style="
                display: inline-block;
                background-color: #3478f5;
                width: 3px;
                height: 20px;
                margin-right: 5px;
                float: left;
                margin-top: 2px;
              "
            ></span>
            可担任首席合伙人商家列表
          </span>
        </div>
      </template>
      <el-card>
        <el-table
          v-loading="loading"
          :data="changeBrandList"
          :header-cell-style="{ background: '#F4F4F5', color: '#606266' }"
          border
        >
          <el-table-column label="序号" type="index" width="60" align="center">
            <template scope="scope">
              <span>{{ scope.$index + 1 }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="商户ID"
            prop="userid"
            align="center"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="公司名称"
            prop="companyname"
            align="center"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="手机号"
            prop="mobile"
            align="center"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="总业绩"
            prop="totalperformance"
            align="center"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="是否已发起申请"
            prop="auditstatus"
            align="center"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.auditstatus == 0">（审核中）</span>
              <span v-else-if="scope.row.auditstatus == 1">（通过）</span>
              <span v-else-if="scope.row.auditstatus == 2">（驳回）</span>
              <span v-else>（已生效）</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                @click.native="changePartner(scope.row)"
                >添加到变更申请
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="hideCompanyForm">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  deleteDailyNew,
  batchAddDailyNew,
  getCompanyPosition,
} from "../../../api/activityCommodity/activityCommodity.js";
import {
  getApplyCeoWishList,
  applyChangeCEO,
  lastCeoChangeRecord,
} from "../../../api/companyManager";
export default {
  components: {},
  data() {
    return {
      activeName: "first", //选项卡默认选择
      companyList: {},
      formData: {
        enterprisename: "孵化",
        usermobile: "xxxxxxx",
        time: new Date(),
      },
      partnerform: {
        //首席合伙人信息
        mobile: "",
        companyname: "",
      },
      companyVisible: false,
      query: {
        currentPage: 1,
        pageSize: 10,
      },
      changeBrandList: [], //有意向公司的列表
      loading: false,
      rules: {
        mobile: [
          { required: true, message: "请添加可变更合伙人", trigger: "change" },
        ],
        companyname: [
          { required: true, message: "请添加可变更合伙人", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    /**
     * 确认首席合伙人变更
     */
    onSubmitPartner(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm("确定要发起首席合伙人变更申请?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
          })
            .then(() => {
              this.applyChangeCEO();
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消",
              });
            });
        } else {
          return false
        }
      });
    },
    /**
     * 添加到变更申请
     */
    changePartner(val) {
      this.partnerform = val;
      this.companyVisible = false;
    },
    /**
     * 关闭弹窗
     */
    hideCompanyForm() {
      this.companyVisible = false;
      this.seniorpartner = [];
    },
    /**
     * 打开弹窗
     */
    handleCompanyForm(val) {
      this.companyVisible = true;
      this.loading = true;
      let data = {
        companyid: this.$route.query.companyId,
      };
      getApplyCeoWishList(data)
        .then((data) => {
          //  console.log(data)
          if (data.code != 0) {
            this.$message.error(data.message);
            this.loading = false;
            return false;
          }
          this.changeBrandList = data.data || [];
          // console.log(this.changeBrandList)
          setTimeout(() => {
            this.loading = false;
          }, 0.3 * 1000);
        })
        .catch((err) => {
          this.loading = false;
          this.changeBrandList = [];
        });
    },
    //下级公司详情发起申请
    applyChangeCEO() {
      this.partnerform.formercompanyid = this.$route.query.companyId; //重新赋值申请变更的公司id
      let data = this.partnerform; //点击哪一个变成哪一个
      applyChangeCEO(data)
        .then((data) => {
          //  console.log(data)
          if (data.code != 0) {
            this.$message.error(data.message);
            this.loading = false;
            return false;
          } else {
            this.$message.success("恭喜您已申请变更");
            this.$router.push({
              path: "myCompany",
              query: { activeName: this.$route.query.activeName },
            });
          }
        })
        .catch((err) => {
          this.$message.error("请求超时");
        });
    },
    lastCeoChangeRecord() {
      let data = {
        companyId: this.$route.query.companyId,
      };
      lastCeoChangeRecord(data)
        .then((data) => {
          if (data.code != 0) {
            this.$message.error(data.message);
            return false;
          }
          this.formData = data.data || {};
        })
        .catch((err) => {
          this.$message.error("请求超时");
        });
    },
  },
  created() {},
  mounted() {
    this.companyList = this.$route.query;
    this.lastCeoChangeRecord();
    console.log(this.$route.query);
  },
};
</script>
<style lang="scss"  scoped>
.title {
  font-size: 24px;
  font-weight: bold;
  margin: 20px 0 20px 10%;
}
</style>
